﻿using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Info;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using SmartMobile.Data;
using SmartMobile.Data;
using SMARTMobile.Common;
using SMARTMobile.Framework;
using SMARTMobile.Models;
using SMARTMobile.Models.Local;
using SMARTMobile.Models.Xml;
using SMARTMobile.Services.SMARTMobileServiceReference;
using SMARTMobile.WinPhone;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Linq;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using System.Xml.Linq;

namespace SMARTMobile.ViewModels
{
    public partial class DuyetTTLamTuyenViewModel : ViewModelBase
    {
        SmartMobileDBContext _smartMobileContext;
        //private ObservableCollection<ARSaleSPModel> _saleCollection;
        //private ARSaleSPModel _selectedSale;
        private ObservableCollection<ARSALESP> _arSaleSPObservable;
        private ARSALESP _selectedARSaleSP;
        private List<ARSALESP> _arSaleSPList;
        private ObservableCollection<string> _dayCollection;
        private string _selectedDay;

        private bool _isShopVisitPopupVisible = false;
        private List<IMAGETYPE> imagetypes;
        private IMAGETYPE _selectedImageType;

        #region Constructors

        public DuyetTTLamTuyenViewModel()
        {
            this.CreateConnection();

            this.ArSaleSPObservable = new ObservableCollection<SmartMobile.Data.ARSALESP>();
            ARSALESP arsale = new ARSALESP
            {
                ID_PK = Guid.NewGuid(),
                Salesp_name = "Chọn nhân viên",
                Salesp_key = "00",
                Sitecode = "00"
            };

            this.ArSaleSPObservable.Add(arsale);
            foreach (var item in this._smartMobileContext.ARSALESP)
            {
                this.ArSaleSPObservable.Add(item);
            }

            if (this.ArSaleSPObservable.Any())
            {
                this.SelectedARSaleSP = this.ArSaleSPObservable[0];
            }

            this.SelectedARSaleSP = this.ArSaleSPObservable[0];

            this.DayCollection = new ObservableCollection<string>
            {
                Localization.AppResources.ROUTECHECK_LABEL_ALL,
                Localization.AppResources.ROUTECHECK_LABEL_T2,
                Localization.AppResources.ROUTECHECK_LABEL_T3,
                Localization.AppResources.ROUTECHECK_LABEL_T4,
                Localization.AppResources.ROUTECHECK_LABEL_T5,
                Localization.AppResources.ROUTECHECK_LABEL_T6,
                Localization.AppResources.ROUTECHECK_LABEL_T7
            };


            this.SelectedDay = this.DayCollection.First();

            if (base.IsNetworkAvailable)
            {
                // get ARSaleSP by SSCode and SiteCode
            }

            this.Imagetypes = GetImageType();
        }

        public event EventHandler SetMapRouteEventHandler;

        #endregion

        #region Properties

        public List<ARSALESP> ARSALESPList
        {
            get
            {
                return this._arSaleSPList;
            }
            set
            {
                this._arSaleSPList = value;
                RaisePropertyChanged("ARSALESPList");
            }
        }

        public ARSALESP SelectedARSaleSP
        {
            get { return this._selectedARSaleSP; }
            set
            {
                this._selectedARSaleSP = value;
                base.RaisePropertyChanged("SelectedARSaleSP");
            }
        }

        public ObservableCollection<ARSALESP> ArSaleSPObservable 
        {
            get { return this._arSaleSPObservable; }
            set
            {
                this._arSaleSPObservable = value;
                base.RaisePropertyChanged("ArSaleSPObservable");
            }
        }


        public string Today
        {
            get { return string.Format("{0:ddd, M/d/yyyy}", DateTime.Now); }
        }

        public bool IsShopVisitPopupVisible
        {
            get { return this._isShopVisitPopupVisible; }
            set
            {
                this._isShopVisitPopupVisible = value;
                base.RaisePropertyChanged("IsShopVisitPopupVisible");
            }
        }

        public ObservableCollection<string> DayCollection
        {
            get { return this._dayCollection; }
            set
            {
                this._dayCollection = value;
                base.RaisePropertyChanged("DayCollection");
            }
        }

        public string SelectedDay
        {
            get { return this._selectedDay; }
            set
            {
                this._selectedDay = value;

                //if (this.SelectedSale.Key != string.Empty && value != Localization.AppResources.ROUTECHECK_LABEL_ALL)
                //{
                //    this.IsShopVisitPopupVisible = true;

                //    // TODO: load map pin-points here
                //    // TODO: draw route on the map
                //}

                if (value != null && this.SetMapRouteEventHandler != null)
                {
                    this.SetMapRouteEventHandler(this, null);
                }

                base.RaisePropertyChanged("SelectedDay");
            }
        }

        public List<IMAGETYPE> Imagetypes
        {
            get { return imagetypes; }
            set
            {                
                imagetypes = value;
                base.RaisePropertyChanged("Imagetypes");
            }
        }

        public IMAGETYPE SelectedImageType
        {
            get { return _selectedImageType; }
            set
            {
                _selectedImageType = value;
                base.RaisePropertyChanged("SelectedImageType");
            }
        }

        //public ObservableCollection<ARSaleSPModel> SaleCollection
        //{
        //    get { return this._saleCollection; }
        //    set
        //    {
        //        this._saleCollection = value;
        //        base.RaisePropertyChanged("SaleCollection");
        //    }
        //}

        //public ARSaleSPModel SelectedSale
        //{
        //    get { return this._selectedSale; }
        //    set
        //    {
        //        this._selectedSale = value;

        //        if (value.Key != string.Empty && this.SelectedDay != Localization.AppResources.ROUTECHECK_LABEL_ALL)
        //        {
        //            this.IsShopVisitPopupVisible = true;

        //            // TODO: load map pin-points here
        //            // TODO: draw route on the map
        //        }

        //        base.RaisePropertyChanged("SelectedSale");
        //    }
        //}

        #endregion

        #region Function

        private void CreateConnection()
        {
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;
        }

        private List<IMAGETYPE> GetImageType()
        {
            try
            {
                // It will copy from local to iso
                _smartMobileContext.CreateIfNotExists();

                // log sql query native on Output window
                _smartMobileContext.LogDebug = true;

                // Query - Same with SQL SERVER Linq
                ////List<IMAGETYPE> list = new List<IMAGETYPE>();
                ////IMAGETYPE newImagetype = new IMAGETYPE();
                ////newImagetype.ID_PK= new Guid();
                ////newImagetype.Description = "Test";
                ////list.AddRange(_smartMobileContext.IMAGETYPE.ToList());
                ////list.Add(newImagetype);
                return _smartMobileContext.IMAGETYPE.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion
    }
}
